import logging
import datetime
import copy
import model
import utility
import config

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class MainPage(webapp.RequestHandler):
    def get(self):
        #self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        self.response.headers['Content-Type'] = 'text/html;'
        query = self.request.get('q', '')
        if query:
            #model.UserLog(useraddr = str(self.request.remote_addr), searchhit = 1, lastvisit = datetime.datetime.now()).record()
            payload = {}
            newfilters = []
            newgroupviews = []
            count = 0
            groups = []

            results = utility.get_google_search_results(query)
            #results += utility.get_google_search_results(query, 100)            
            count = len(results)

            seal = utility.seal_query_xml(query, results)
            results2 = utility.get_cluster_results(c2stream=seal)

            resp = results2
        else:
            resp = template.render('form.html', {})
        self.response.out.write(resp)

